package com.xingkeduo.dto.transter.activitycardlist;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.utils.DateUtils;

import lombok.Data;

@Data
public class Activity_Card_list {
	private String id;
	private String name;
	private String pic;
	private String type;
	private String use_store;
	private String date;
	private String start_date;
	private String end_date;
	private String card_type;
	private List<String> reduce_cost;
	private List<String> discount;
	private List<String> cant_use_reason;
	private Boolean need_check;
	private String cert_status;

	public void covert(JSONObject json) {
		this.id = json.getOrDefault("id", "")==null?"":json.getOrDefault("id", "").toString();
		this.name = json.getOrDefault("marketingName", "")==null?"":json.getOrDefault("marketingName", "").toString();
		this.type = "1";
		this.pic="common";
		//this.use_store =json.getOrDefault("storeNames", "")==null?"": json.getOrDefault("storeNames", "").toString();
		String storeIds = json.getOrDefault("storeIds", "")==null?"":json.getOrDefault("storeIds", "").toString();
		this.use_store="适用部分门店";
		if("0".equals(storeIds)) {
			this.use_store="适用所有门店";
		}
		Date startDate = json.getDate("useStartTime");
		if (startDate != null) {
			this.start_date = DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT_V3);
		}
		Date endDate = json.getDate("useEndTime");
		if (endDate != null) {
			this.end_date = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);
		}
		this.card_type = "1";
		String serviceType = json.getOrDefault("serviceType", "")==null?"":json.getOrDefault("serviceType", "").toString();
		String couponAmount = json.getOrDefault("couponAmount", "")==null?"":json.getOrDefault("couponAmount", "").toString();
		if (StringUtils.isBlank(couponAmount)) {
			this.reduce_cost = Lists.newArrayList("0", "0");
		} else {
			BigDecimal amount = new BigDecimal(couponAmount);
			if("DISCOUNT".equalsIgnoreCase(serviceType)) {
				amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
				this.discount = Lists.newArrayList(one+"",two+"");
			}else{
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
				two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
				this.reduce_cost = Lists.newArrayList(one+"",(two+"").replaceAll("\\.", ""));
			}
		}
		String unusableReason = json.getOrDefault("unusableReason", "")==null?"":json.getOrDefault("unusableReason", "").toString();
		if (StringUtils.isNotBlank(unusableReason)) {
			this.cant_use_reason = Lists.newArrayList(unusableReason);
		}

		/** 认证状态，0:未认证,1:已认证,2:已冻结,3已作废 */

		String authStatus = json.getOrDefault("authStatus", "")==null?"":json.getOrDefault("authStatus", "").toString();
		if (StringUtils.isNotBlank(authStatus)) {
			if ("FREE".equalsIgnoreCase(serviceType)) {
				this.need_check = Boolean.TRUE;
			} else if("DISCOUNT".equalsIgnoreCase(serviceType) &&authStatus.equals("0")){
				this.need_check = Boolean.TRUE;
			}else {
				this.need_check = Boolean.FALSE;
			}
			if ("0".equals(authStatus)) {
				this.cert_status = "0";
			} else if ("1".equals(authStatus)) {
				this.cert_status = "1";
			} else if ("2".equals(authStatus)) {
				this.cert_status = "-1";
			} else if ("3".equals(authStatus)) {
				this.cert_status = "2";
			}
		}

	}

}
